import { MAP_CONFIG } from './config.js'

function addPoint(point, docLayer) {
  //1.构建点要素的几何体信息
  const gPoint = new Zondy.Object.GPoint(point[0], point[1])
  const fGeom = new Zondy.Object.FeatureGeometry({
    PntGeom: [gPoint],
  })
  //2.设置样式信息
  const pointInfo = new Zondy.Object.CPointInfo(MAP_CONFIG.pointStyle)
  //3.设置当前要素的图形参数信息
  const webGraphicInfo = new Zondy.Object.WebGraphicsInfo({
    InfoType: 1, //1=>point 2=>line 3=>aarea
    PntInfo: pointInfo,
  })
  //4.设置要素的自定义属性
  const attValue = ['武汉']
  //5.创建一个要素
  const feature = new Zondy.Object.Feature({
    fGeom: fGeom,
    GraphicInfo: webGraphicInfo,
    AttValue: attValue,
  })
  //6.设置要素为点要素
  feature.setFType(1)
  //7.创建一个要素数据集
  const featureSet = new Zondy.Object.FeatureSet()
  //8.设置属性结构，根据图层属性进行设置
  const cAttStruct = new Zondy.Object.CAttStruct({
    FldName: ['name'],
    FldNumber: 1,
    FldType: ['string'],
  })
  featureSet.AttStruct = cAttStruct
  featureSet.addFeature(feature)
  //调用接口服务 将数据同步到数据库
  // 第一个参数表示服务名称 第二个参数表示当前要素所在图层的索引值
  const addService = new Zondy.Service.EditDocFeature(
    MAP_CONFIG.docName,
    MAP_CONFIG.pointLayerIndex,
    {
      ip: MAP_CONFIG.ip,
      port: MAP_CONFIG.port,
    }
  )
  addService.add(featureSet, onAddPntSuccess)
  function onAddPntSuccess(data) {
    if (data) {
      alert('添加点要素成功！')
      //刷新图层
      docLayer.refresh()
    } else {
      alert('添加点要素失败！')
    }
  }
}

export default addPoint
